package Z.dailyExercise.March;

import java.util.Stack;

public class _剑指Offer31栈的压入弹出序列 {

    /**
     * 定义一个栈 从pushed数组中按个push 知道等于poped元素
     * @param pushed
     * @param popped
     * @return
     */

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i=0,j=0;
       while (i< pushed.length){

            while (i < pushed.length && pushed[i] != popped[j]){
                stack.push(pushed[i]);
                i++;
            }
            j++;
            i++;
            while (j< popped.length && !stack.isEmpty() && stack.peek() == popped[j] ){
                stack.pop();
                j++;
            }


        }

       if (stack.isEmpty()){
           return true;
       }else {
           return false;
       }

    }
}
